/**
 * 提现状态
 */

export const state = () => {
  let form = sessionStorage.getItem("wealth-draw-form");
  let isCustom = sessionStorage.getItem("wealth-draw-iscustom");
  let status = sessionStorage.getItem("wealth-draw-status");
  let lock = sessionStorage.getItem("wealth-draw-lock");


  try {
    form = JSON.parse(form);
  } catch (e) {
    form = null;
  }

  try {
    isCustom = Boolean(isCustom)
  } catch (e) {
    isCustom = false;
  }

  try {
    lock = Boolean(lock)
  } catch (e) {
    lock = true;
  }

  try {
    status = JSON.parse(status);
  } catch (e) {
    status = null;
  }

  return {
    status,
    form, //表单
    lock,
    isCustom
  }
}

export const mutations = {
  setIsCustom(state, isCustom) {
    if (isCustom) {
      sessionStorage.setItem("wealth-draw-iscustom", isCustom);
    } else {
      sessionStorage.removeItem("wealth-draw-iscustom");
    }
    state.isCustom = isCustom;
  },
  setLock(state, lock) {
    if (lock) {
      sessionStorage.setItem("wealth-draw-lock", lock);
    } else {
      sessionStorage.removeItem("wealth-draw-lock");
    }
    state.lock = lock;
  },
  setForm(state, form) {
    if (form) {
      sessionStorage.setItem("wealth-draw-form", JSON.stringify(form));
    } else {
      sessionStorage.removeItem("wealth-draw-form");
    }
    state.form = form || null
  },
  setStatus(state, status) {
    if (status) {
      sessionStorage.setItem("wealth-draw-status", JSON.stringify(status));
    } else {
      sessionStorage.removeItem("wealth-draw-status");
    }
    state.status = status || null
  },
}

export const actions = {
  clear({
    commit
  }) { // 清楚所有状态
    commit('setStatus', null)
    commit('setForm', null)
    commit('setIsCustom', false)
    commit('setLock', true)
  },
}
